using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using BLL;
using BLL.Impl;

namespace WebUI.CargoCars
{
    public partial class DispatchVehicle : BasicPage
    {

        private IVehicleManager vehicleManager;
        private ICargoCarsManager cargoCarsManager;
        private int cargoCarsSN = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Request.QueryString["CargoCarsSN"] || String.IsNullOrEmpty(Request.QueryString["CargoCarsSN"].Trim()))
                Response.Redirect("~/CargoCars/CargoCars_List.aspx");
            cargoCarsSN = Int32.Parse(Request.QueryString["CargoCarsSN"].Trim());
            if(cargoCarsSN<1)
                Response.Redirect("~/CargoCars/CargoCars_List.aspx");
            vehicleManager = BLLFactory.CreateVehicleManager();
            cargoCarsManager = BLLFactory.CreateCargoCarsManager();
            if (!IsPostBack)
            {
                Display();
            }

        }

        private void Display()
        {
            this.gvwDispatchVehicle.DataSource = cargoCarsManager.SelectVehicle(cargoCarsSN);
            this.gvwDispatchVehicle.DataKeyNames = new string[] { "VehicleSN" };
            this.gvwDispatchVehicle.DataBind();
           
            this.ddrVehicleList.DataSource = cargoCarsManager.SelectVehicle(0);
            this.ddrVehicleList.DataTextField = "VehicleNumber";
            this.ddrVehicleList.DataValueField = "VehicleSN";
            this.ddrVehicleList.DataBind();
        }

        protected void btnVehicleAdd_Click(object sender, EventArgs e)
        {
            int vehicleSn = Int32.Parse(this.ddrVehicleList.SelectedValue.ToString());

            cargoCarsManager.SaveVehicleByCargoCars(cargoCarsSN, vehicleSn);

            Display();
        }

        protected void gvwDispatchVehicle_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int vehicleSn = Int32.Parse(this.gvwDispatchVehicle.DataKeys[e.RowIndex].Value.ToString());
            Model.CargoCars cargoCar=new Model.CargoCars();
            cargoCar.CargoCarsSN=0;
            cargoCarsManager.DelectVehicleByCargoCars(cargoCar, vehicleSn);
            Display();
        }

    }
}
